package com.ruoyi.wemedia.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.wemedia.domain.vo.SensitiveVo;
import com.ruoyi.wemedia.domain.bo.SensitiveBo;
import com.ruoyi.wemedia.service.ISensitiveService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 敏感词控制器
 * 前端访问路由地址为:/wemedia/sensitive
 *
 * @author xiaoqiang
 * @date 2022-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sensitive")
public class SensitiveController extends BaseController {

    private final ISensitiveService iSensitiveService;

    /**
     * 查询敏感词列表
     */
    @SaCheckPermission("wemedia:sensitive:list")
    @GetMapping("/list")
    public TableDataInfo<SensitiveVo> list(SensitiveBo bo, PageQuery pageQuery) {
        return iSensitiveService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出敏感词列表
     */
    @SaCheckPermission("wemedia:sensitive:export")
    @Log(title = "敏感词", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SensitiveBo bo, HttpServletResponse response) {
        List<SensitiveVo> list = iSensitiveService.queryList(bo);
        ExcelUtil.exportExcel(list, "敏感词", SensitiveVo.class, response);
    }

    /**
     * 获取敏感词详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wemedia:sensitive:query")
    @GetMapping("/{id}")
    public R<SensitiveVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSensitiveService.queryById(id));
    }

    /**
     * 新增敏感词
     */
    @SaCheckPermission("wemedia:sensitive:add")
    @Log(title = "敏感词", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SensitiveBo bo) {
        return toAjax(iSensitiveService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改敏感词
     */
    @SaCheckPermission("wemedia:sensitive:edit")
    @Log(title = "敏感词", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SensitiveBo bo) {
        return toAjax(iSensitiveService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除敏感词
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wemedia:sensitive:remove")
    @Log(title = "敏感词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSensitiveService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
